import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useMembersStore } from './members'
import { useMemberCardStore } from './memberCard'
import { useClassScheduleStore } from './classSchedule'

export const useReservationRecordStore = defineStore('reservationRecord',() => {
    const membersStore = useMembersStore()
    const memberCardStore = useMemberCardStore()
    const scheduleStore = useClassScheduleStore()
    // 模拟预约记录数据
    const reservationRecords = ref([
        {
            memberId: 1,
            scheduleId: 1,
            reservationTime: '2022-01-01 10:00:00',
            memberCardId: 1,
            peopleCount: 2,
            consumeTimes: 3,
            operationTime: '2022-01-01 10:00:00',
            operator: '张老师',
            remark: '',
            type: '已预约'
        }
    ])

    const getActiveMembers = () => {
        let res = 0
        const set = {}
        reservationRecords.value.forEach(record => {
            if (set[record.memberId] === undefined) {
                set[record.memberId] = true
                res += 1
            }
        })
        return res
    }

    const getReservationRecordsByMemberId = (memberId) => {
        const res = []
        reservationRecords.value.forEach(record => {
            if (record.memberId === memberId) {
                let memberCard = memberCardStore.getMemberCardById(record.memberCardId)
                let schedule = scheduleStore.getClassScheduleById(record.scheduleId)
                res.push({
                    memberCardName: memberCard.memberCardName,
                    courseName: schedule.courseName,
                    type: record.type,
                    reservationTime: record.reservationTime,
                    operationTime: record.operationTime,
                    operator: record.operator,
                    remark: record.remark,
                    peopleCount: record.peopleCount,
                    consumeTimes: record.consumeTimes
                })
            }
        })
        return res
    }

    const anyReservationRecordByCourseId = (courseId) => {
        return reservationRecords.value.some(record => record.courseId === courseId)
    }

    const getRecordsByScheduleId = (scheduleId) => {
        const res = []
        reservationRecords.value.forEach(record => {
            if (record.scheduleId === scheduleId) {
                let member = membersStore.getMemberById(record.memberId)
                let memberCard = memberCardStore.getMemberCardById(record.memberCardId)
                res.push({
                    memberName: member.memberName,
                    phoneNum: member.phoneNum,
                    memberCardName: memberCard.memberCardName,
                    peopleCount: record.peopleCount,
                    consumeTimes: record.consumeTimes,
                    operationTime: record.operationTime,
                    operator: record.operator,
                    type: record.type,
                })
            }
        })
        return res
    }

    const addRecord = (form, scheduleId, consumeTimes) => {
        reservationRecords.value.push({
            memberId: form.memberId,
            scheduleId: scheduleId,
            reservationTime: formatDateTime(new Date()),
            memberCardId: form.memberCardId,
            peopleCount: form.peopleCount,
            consumeTimes: consumeTimes,
            operationTime: formatDateTime(new Date()),
            operator: '张老师',
            remark: form.remark,
            type: '已预约'
        })
    }

    const formatDateTime = (date) => {
        var year = date.getFullYear();
        var month = (1 + date.getMonth()).toString().padStart(2, '0');
        var day = date.getDate().toString().padStart(2, '0');
        var hours = date.getHours().toString().padStart(2, '0');
        var minutes = date.getMinutes().toString().padStart(2, '0');
        var seconds = date.getSeconds().toString().padStart(2, '0');
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    }

    return{
        reservationRecords,
        getReservationRecordsByMemberId,
        getActiveMembers,
        getRecordsByScheduleId,
        anyReservationRecordByCourseId,
        addRecord
    }
})
